/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import dao.GastoDao;
import dao.TipoGastoDao;
import dao.UtilGeral;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Gasto;
import model.OperacoesEnum;
import model.TipoGasto;
import model.Usuario;

/**
 *
 * @author Jeisse
 */
public class GastoServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String operacao = request.getParameter("operacao");
        if ("FormularioIncluir".equalsIgnoreCase(operacao)) {
            this.incluiGasto(request);
            RequestDispatcher disp = request.getRequestDispatcher("index.jsp");
            disp.forward(request, response);

        }

        if ("FormularioAlterar".equalsIgnoreCase(operacao)) {
            this.editarGasto(request);
            RequestDispatcher disp = request.getRequestDispatcher("index.jsp");
            disp.forward(request, response);

        }

        if ("FormularioExcluir".equalsIgnoreCase(operacao)) {
            this.excluirGasto(request);
            RequestDispatcher disp = request.getRequestDispatcher("index.jsp");
            disp.forward(request, response);

        }

        //se for uma operação que vai necessitar da lista de tipo gasto
        if (OperacoesEnum.INCLUIR.getNome().equalsIgnoreCase(operacao)
                || OperacoesEnum.ALTERAR.getNome().equalsIgnoreCase(operacao)) {
            List<TipoGasto> listaTipoGasto = new TipoGastoDao().getLista();
            request.getSession().setAttribute("listaTipoGasto", listaTipoGasto);
        }

        if (OperacoesEnum.INCLUIR.getNome().equalsIgnoreCase(operacao)) {
            RequestDispatcher disp = request.getRequestDispatcher("incluiGasto.jsp");
            disp.forward(request, response);
        }

        if (OperacoesEnum.ALTERAR.getNome().equalsIgnoreCase(operacao)) {
            Integer idGasto = Integer.valueOf(request.getParameter("registro"));
            if (OperacoesEnum.ALTERAR.getNome().equalsIgnoreCase(operacao)) {

                GastoDao gd = new GastoDao();
                Gasto gasto = gd.getGastoById(idGasto);

                HttpSession session = request.getSession();
                session.setAttribute("gasto", gasto);

                RequestDispatcher disp = request.getRequestDispatcher("EdicaoGasto.jsp");
                disp.forward(request, response);
            }
        }
        
         if(OperacoesEnum.EXCLUIR.getNome().equalsIgnoreCase(operacao)){
            this.excluirGasto(request);
            RequestDispatcher disp = request.getRequestDispatcher("index.jsp");
            disp.forward(request, response);  
         }

        
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void incluiGasto(HttpServletRequest request) throws ServletException, IOException {
        Gasto gasto = new Gasto();
        String stringData = request.getParameter("data");
        if (stringData != null && !stringData.isEmpty()) {
            gasto.setDtGasto(UtilGeral.converteDataViewParaBanco(stringData));
        }
        String idTipoGasto = request.getParameter("tipoGasto");
        if (idTipoGasto != null && !idTipoGasto.isEmpty()) {
            TipoGasto tipoGasto = new TipoGastoDao().getTipoGastoById(Integer.parseInt(idTipoGasto));
            gasto.setTipoGasto(tipoGasto);
        }
        gasto.setUsuario((Usuario) request.getSession().getAttribute("usuario"));
        new GastoDao().insere(gasto);

    }

    private void editarGasto(HttpServletRequest request) throws ServletException, IOException {
        Gasto gasto = new Gasto();
        String stringData = request.getParameter("data");

        Integer id = Integer.valueOf(request.getParameter("idGasto"));
        gasto.setId(id);

        if (stringData != null && !stringData.isEmpty()) {
            gasto.setDtGasto(UtilGeral.converteDataViewParaBanco(stringData));
        }
        String idTipoGasto = request.getParameter("tipoGasto");
        if (idTipoGasto != null && !idTipoGasto.isEmpty()) {
            TipoGasto tipoGasto = new TipoGastoDao().getTipoGastoById(Integer.parseInt(idTipoGasto));
            gasto.setTipoGasto(tipoGasto);
        }

        gasto.setUsuario((Usuario) request.getSession().getAttribute("usuario"));
        new GastoDao().atualiza(gasto);

    }

    private void excluirGasto(HttpServletRequest request) {
        Integer id = Integer.valueOf(request.getParameter("registro"));
        GastoDao gD = new GastoDao();
        Gasto gasto = gD.getGastoById(id);
        gD.remove(gasto);
    }
}
